import { reactive, ref } from 'vue'
import { getDeptList } from '@/api/org-manage/depart-manage/index'
import { getDeptPerson } from '@/api/system/user.js'

// 部门
const deptList = ref([])

// 人员
const personList = ref([])

export const form = reactive({
  userForm: {
    assignDeptId: null,
    assignUserIds: []
  },
  userRules: {
    assignDeptId: [{ required: true, message: '请选择部门名称', trigger: 'blur' }],
    assignUserIds: [{ required: true, type: 'array', message: '请选择人员', trigger: 'blur' }]
  },
  formItem: [
    { type: 'select', key: 'assignDeptId', label: '部门名称', props: { options: deptList, placeholder: '请选择部门名称', clearable: true }},
    { type: 'select-multiple', key: 'assignUserIds', label: '人员姓名', props: { options: personList, placeholder: '请选择人员姓名', clearable: true }}
  ]
})

// 部门 列表
export const getDeptInfo = async function(id) {
  const { data } = await getDeptList(id)
  deptList.value = data?.map(it => ({
    ...it,
    label: it.name,
    value: it.id
  }))
}

// 人员 列表
export const getPerson = async function(id) {
  const { data } = await getDeptPerson(id)
  personList.value = data?.map(it => ({
    ...it,
    label: it.realname,
    value: it.id
  }))
}
